package com.ylr.base.common.enums;

import com.ylr.base.common.dto.NameValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2023-04-18 22:17:04
 * className: ChannelEnum 请求渠道枚举类
 * version: 1.0
 * description:
 */
@Getter
@AllArgsConstructor
public enum ChannelEnum {

    WEB(0, "web", "浏览器"),
    H5(1, "h5", "H5"),
    APP(2, "app", "App"),
    WECHAT_APPLET(3, "wechatApplet", "微信小程序"),
    ZFB_APPLET(4, "zfbApplet", "支付宝小程序"),
    ;

    /**
     * 值
     */
    private final Integer value;
    /**
     * 编码
     */
    private final String code;
    /**
     * 名称
     */
    private final String name;

    /**
     * 获取渠道枚举对象
     * 根据渠道编码，获取对应渠道枚举对象
     * @param code 渠道编码
     * @return 渠道枚举对象
     */
    public static ChannelEnum getChannelEnum(String code) {
        for (ChannelEnum channelEnum : values()) {
            if (channelEnum.code.equals(code)) {
                return channelEnum;
            }
        }
        return null;
    }

    /**
     * 获取渠道枚举对象
     * 根据渠道值，获取对应渠道枚举对象
     * @param value 渠道值
     * @return 渠道枚举对象
     */
    public static ChannelEnum getChannelEnum(Integer value) {
        for (ChannelEnum channelEnum : values()) {
            if (channelEnum.value.equals(value)) {
                return channelEnum;
            }
        }
        return null;
    }

    /**
     * 获取渠道枚举对象列表
     */
    public static List<NameValue<Integer>> getchannelEnumList() {
        return Stream.of(values())
                .map(channelEnum -> new NameValue<>(channelEnum.name, channelEnum.value))
                .collect(Collectors.toList());
    }
}
